if(!ARGV[0] || !ARGV[1] || !ARGV[2])
	puts "Run 'info.rb oldRatings.txt newRatings.txt attendees.txt'"
	exit
end

oldRatings = {}
newRatings = {}

def getRatingsFromFile(fileName)
	ratings = {}
	fin = File.new(fileName, "r")
	while(line = fin.gets)	
		if(/^(.+?)\s+([\d.]+)/.match(line))		
			ratings[$1] = $2.to_f;
		end
	end
	fin.close
	return ratings;
end


def getPlayersFromList(fileName)
	playersFile = File.new(fileName, "r")
	players = playersFile.readlines;
	playersFile.close;
	players.each {|player| player.rstrip!.lstrip! }
	players;	
end

attendedPlayers = getPlayersFromList(ARGV[2]);
oldRatings = getRatingsFromFile(ARGV[0]);
newRatings = getRatingsFromFile(ARGV[1]);

addedPoints = {};
newPlayers = [];

newRatings.keys.each { |key|
	if(!oldRatings[key])
		newPlayers << key;
		oldRatings[key] = 0.0;
	end
	addedPoints[key] = newRatings[key].round - oldRatings[key].round;	
}

ratingChanges = {};
attendedPlayers.each {|player|
	ratingChanges[player] = addedPoints[player];
}

$fileName = "ratings-info-" + (Date.today.to_s) + ".txt";
fout = File.new($fileName, "w")
fout << "Rating changes:\n";

ratingChanges.sort{|a,b| b[1] <=> a[1]}.each {|key, value| 
	fout <<  key.to_s + " "*(20 - key.length) + value.to_s + "\n";
}
fout << "\nNew players:\n";
newPlayers.each {|key| fout << key << "\n"; }
fout.close
